﻿using System.Threading.Tasks;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Mp.GroupSend.Entity;
using static System.String;
namespace WeShare.WeiXin.Mp.GroupSend
{
    public class GroupSendApi
    {

        /// <summary>
        /// 上传视频素材
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <param name="mediaId"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static UploadRes UpLoadVideo(string mediaId, string title, string description, string tokenOrAppId = "")
        {
            var url =
                $"https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var json = new
            {
                media_id = mediaId,
                title = title,
                description = description
            };
            return HttpUtil.PostResult<UploadRes>(json, url);
        }

        /// <summary>
        /// 基础发送接口
        /// </summary>
        /// <param name="obj">json对象</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="sendType">群发类型，1为按分组群发，2为按openid列表群发，3为
        ///预览接口。默认为按分组群发</param>
        private static SendRes BaseSend(string tokenOrAppId, object obj, int sendType = 1)
        {
            string url = null;
            switch (sendType)
            {
                //分组群发
                case 1:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={OpenApi.GetToken(tokenOrAppId)}"; break;
                //openid列表群发
                case 2:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={OpenApi.GetToken(tokenOrAppId)}"; break;
                //预览接口
                case 3:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={OpenApi.GetToken(tokenOrAppId)}"; break;
                default:
                    url = $"https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={OpenApi.GetToken(tokenOrAppId)}"; break;
            }
            return HttpUtil.PostResult<SendRes>(obj, url);
        }

        /// <summary>
        ///  按分组群发图文消息，isall为true时，说明群发所有用户，此时tag_id可为空； 否则，根据tag_id进行群发
        /// </summary>
        /// <param name="mediaId">图片的媒体ID</param>
        /// <param name="isall">是否群发所有用户</param>
        /// <param name="tagId">分组ID</param>
        /// <param name="clientmsgid">使用 clientmsgid 参数，避免重复推送</param>
        /// <param name="sendIgnoreReprint"></param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        public static SendRes SendArticle(string mediaId, int tagId = 0, string clientmsgid = "", int sendIgnoreReprint = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = "mpnews",
                send_ignore_reprint = sendIgnoreReprint
            };
            return BaseSend(tokenOrAppId, json);
        }


        public static BaseRes SendText(string content, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                text = new
                {
                    content = content
                },
                msgtype = "text"
            };
            return BaseSend(tokenOrAppId, json);
        }
        /// <summary>
        /// 根据分组群发卡券
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="tokenOrAppId"></param>
        /// <param name="tagId"></param>
        /// <returns></returns>
        public static BaseRes SendCard(string cardId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                wxcard = new
                {
                    card_id = cardId
                },
                msgtype = "wxcard"
            };
            return BaseSend(tokenOrAppId, json);
        }
        public static BaseRes SendVoice(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                voice = new
                {
                    media_id = mediaId
                },
                msgtype = "voice"
            };
            return BaseSend(tokenOrAppId, json);
        }
        public static BaseRes SendImg(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                image = new
                {
                    media_id = mediaId
                },
                msgtype = "image"
            };
            return BaseSend(tokenOrAppId, json, 1);
        }
        public static BaseRes SendVideo(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                mpvideo = new
                {
                    media_id = mediaId
                },
                msgtype = "mpvideo"
            };
            return BaseSend(tokenOrAppId, json);
        }

        #region 按openid列表群发和预览接口“订阅号不可用，服务号认证后可用”
        /// <summary>
        /// 按用户列表群发文本消息
        /// </summary>
        /// <param name="content">群发内容</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表 示openid，调用的是预览接口</param>
        public static BaseRes SendTextByOpenId(string content, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                text = new
                {
                    content = content
                },
                msgtype = "text"
            };
            return BaseSend(tokenOrAppId,json, isPreview ? 3 : 2);
        }

        /// <summary>
        /// 根据openid列表群发卡券
        /// </summary>
        /// <param name="cardId">卡券编号</param>
        /// <param name="tokenOrAppId"></param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表 示openid，调用的是预览接口</param>
        /// <returns></returns>
        public static BaseRes SendCardByOpenId(string cardId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                wxcard = new
                {
                    card_id = cardId
                },
                msgtype = "wxcard"
            };
            return BaseSend(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发图片消息
        /// </summary>
        /// <param name="mediaId">图片媒体ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的群发接口；否则表示openid，调用的是预览接口</param>
        public static BaseRes SendImgByOpenId(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                image = new
                {
                    media_id = mediaId
                },
                msgtype = "image"
            };
            return BaseSend(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发图片消息
        /// </summary>
        /// <param name="mediaId">语音媒体ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表
        ///示openid，调用的是预览接口</param>
        public static BaseRes SendVoiceByOpenId(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                voice = new
                {
                    media_id = mediaId
                },
                msgtype = "voice"
            };
            return BaseSend(tokenOrAppId, json, isPreview ? 3 : 2);
        }

        ///  <summary>
        ///  按用户列表群发图文消息
        ///  </summary>
        /// <param name="mediaId">图文ID</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表
        ///     示openid，调用的是预览接口</param>
        /// <param name="sendIgnoreReprint">	图文消息被判定为转载时，是否继续群发。 1为继续群发（转载），0为停止群发。 该参数默认为0。</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        public static SendRes SendArticleByOpenId(string mediaId, object touser, int sendIgnoreReprint = 0,
            string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = "mpnews",
                send_ignore_reprint = sendIgnoreReprint

            };
            return BaseSend(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发视频消息
        /// </summary>
        /// <param name="mediaId">视频ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的群是发接口；否则表
        ///示openid，调用的是预览接口</param>
        public static BaseRes SendVideoByOpenId(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                mpvideo = new
                {
                    media_id = mediaId
                },
                msgtype = "mpvideo"
            };
            return BaseSend(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        #endregion

        /// <summary>
        /// 查询群发状态
        /// </summary>
        /// <param name="msgId"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static GroupStatus QueryStatus(string msgId, string tokenOrAppId)
        {
            var url =
                $"https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var json = new { msg_id = msgId };
            return HttpUtil.PostResult<GroupStatus>(json, url);
        }

        /// <summary>
        /// 删除群发
        /// </summary>
        /// <param name="msgId">群发消息ID</param>
        /// <param name="articleIdx">要删除的文章在图文消息中的位置，第一篇编号为1，该字段不填或填0会删除全部文章</param>
        /// <param name="tokenOrAppId"></param>
        public static BaseRes Delete(string msgId, int articleIdx = 0, string tokenOrAppId = "")
        {
            var url =
                $"https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var json = new { msg_id = msgId, article_idx = articleIdx };
            return HttpUtil.PostResult<BaseRes>(json, url);
        }
        /// <summary>
        /// 获取群发速度
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static SendSpeed GetSpeed(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.PostResult<SendSpeed>(null, url);
        }
        /// <summary>
        /// 设置群发速度
        /// </summary>
        /// <param name="speed">群发速度的级别，是一个0到4的整数，数字越大表示群发速度越慢。</param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static BaseRes SetSpeed(int speed, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { speed = speed };
            return HttpUtil.PostResult<BaseRes>(data, url);
        }

#if !NET40
        /// <summary>
        /// 上传视频素材
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <param name="mediaId"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static async Task<UploadRes> UpLoadVideoAsync(string mediaId, string title, string description, string tokenOrAppId = "")
        {
            var url =
                $"https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var json = new
            {
                media_id = mediaId,
                title = title,
                description = description
            };
            return await HttpUtil.PostResultAsync<UploadRes>(json, url);
        }

        /// <summary>
        /// 基础发送接口
        /// </summary>
        /// <param name="obj">json对象</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="sendType">群发类型，1为按分组群发，2为按openid列表群发，3为
        ///预览接口。默认为按分组群发</param>
        private static async Task<SendRes> BaseSendAsync(string tokenOrAppId, object obj, int sendType = 1)
        {
            string url = null;
            switch (sendType)
            {
                //分组群发
                case 1:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}"; break;
                //openid列表群发
                case 2:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}"; break;
                //预览接口
                case 3:
                    url =
                        $"https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}"; break;
                default:
                    url = $"https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}"; break;
            }
            return await HttpUtil.PostResultAsync<SendRes>(obj, url);
        }

        /// <summary>
        ///  按分组群发图文消息，isall为true时，说明群发所有用户，此时tag_id可为空； 否则，根据tag_id进行群发
        /// </summary>
        /// <param name="mediaId">图片的媒体ID</param>
        /// <param name="isall">是否群发所有用户</param>
        /// <param name="tagId">分组ID</param>
        /// <param name="clientmsgid">使用 clientmsgid 参数，避免重复推送</param>
        /// <param name="sendIgnoreReprint"></param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        public static async Task<SendRes> SendArticleAsync(string mediaId, int tagId = 0, string clientmsgid = "", int sendIgnoreReprint = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = "mpnews",
                send_ignore_reprint = sendIgnoreReprint
            };
            return await BaseSendAsync(tokenOrAppId, json);
        }


        public static async Task<BaseRes> SendTextAsync(string content, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                text = new
                {
                    content = content
                },
                msgtype = "text"
            };
            return await BaseSendAsync(tokenOrAppId, json);
        }
        /// <summary>
        /// 根据分组群发卡券
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="tokenOrAppId"></param>
        /// <param name="tagId"></param>
        /// <returns></returns>
        public static async Task<BaseRes> SendCardAsync(string cardId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                wxcard = new
                {
                    card_id = cardId
                },
                msgtype = "wxcard"
            };
            return await BaseSendAsync(tokenOrAppId, json);
        }
        public static async Task<BaseRes> SendVoiceAsync(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                voice = new
                {
                    media_id = mediaId
                },
                msgtype = "voice"
            };
            return await BaseSendAsync(tokenOrAppId, json);
        }
        public static async Task<BaseRes> SendImgAsync(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                image = new
                {
                    media_id = mediaId
                },
                msgtype = "image"
            };
            return await BaseSendAsync(tokenOrAppId, json, 1);
        }
        public static async Task<BaseRes> SendVideoAsync(string mediaId, int tagId = 0, string tokenOrAppId = "")
        {
            var json = new
            {
                filter = new
                {
                    is_to_all = tagId == 0 ? true : false,
                    tag_id = tagId
                },
                mpvideo = new
                {
                    media_id = mediaId
                },
                msgtype = "mpvideo"
            };
            return await BaseSendAsync(tokenOrAppId, json);
        }

        #region 按openid列表群发和预览接口“订阅号不可用，服务号认证后可用”
        /// <summary>
        /// 按用户列表群发文本消息
        /// </summary>
        /// <param name="content">群发内容</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表 示openid，调用的是预览接口</param>
        public static async Task<BaseRes> SendTextByOpenIdAsync(string content, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                text = new
                {
                    content = content
                },
                msgtype = "text"
            };
            return await BaseSendAsync(tokenOrAppId,json, isPreview ? 3 : 2);
        }

        /// <summary>
        /// 根据openid列表群发卡券
        /// </summary>
        /// <param name="cardId">卡券编号</param>
        /// <param name="tokenOrAppId"></param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表 示openid，调用的是预览接口</param>
        /// <returns></returns>
        public static async Task<BaseRes> SendCardByOpenIdAsync(string cardId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                wxcard = new
                {
                    card_id = cardId
                },
                msgtype = "wxcard"
            };
            return await BaseSendAsync(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发图片消息
        /// </summary>
        /// <param name="mediaId">图片媒体ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的群发接口；否则表示openid，调用的是预览接口</param>
        public static async Task<BaseRes> SendImgByOpenIdAsync(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                image = new
                {
                    media_id = mediaId
                },
                msgtype = "image"
            };
            return await BaseSendAsync(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发图片消息
        /// </summary>
        /// <param name="mediaId">语音媒体ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表
        ///示openid，调用的是预览接口</param>
        public static async Task<BaseRes> SendVoiceByOpenIdAsync(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                voice = new
                {
                    media_id = mediaId
                },
                msgtype = "voice"
            };
            return await BaseSendAsync(tokenOrAppId, json, isPreview ? 3 : 2);
        }

        ///  <summary>
        ///  按用户列表群发图文消息
        ///  </summary>
        /// <param name="mediaId">图文ID</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的是群发接口；否则表
        ///     示openid，调用的是预览接口</param>
        /// <param name="sendIgnoreReprint">	图文消息被判定为转载时，是否继续群发。 1为继续群发（转载），0为停止群发。 该参数默认为0。</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        public static async Task<SendRes> SendArticleByOpenIdAsync(string mediaId, object touser, int sendIgnoreReprint = 0,
            string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = "mpnews",
                send_ignore_reprint = sendIgnoreReprint
            };
            return await BaseSendAsync(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        /// <summary>
        /// 按用户列表群发视频消息
        /// </summary>
        /// <param name="mediaId">视频ID</param>
        /// <param name="tokenOrAppId">tokenOrAppId</param>
        /// <param name="touser">如果为数组，则表示openid列表，调用的群是发接口；否则表
        ///示openid，调用的是预览接口</param>
        public static async Task<BaseRes> SendVideoByOpenIdAsync(string mediaId, object touser, string tokenOrAppId = "")
        {
            var isPreview = !touser.GetType().IsArray;
            var json = new
            {
                touser = touser,
                towxname = isPreview ? touser.ToString() : "",
                mpvideo = new
                {
                    media_id = mediaId
                },
                msgtype = "mpvideo"
            };
            return await BaseSendAsync(tokenOrAppId, json, isPreview ? 3 : 2);
        }
        #endregion

        /// <summary>
        /// 查询群发状态
        /// </summary>
        /// <param name="msgId"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<GroupStatus> QueryStatusAsync(string msgId, string tokenOrAppId)
        {
            var url =
                $"https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var json = new { msg_id = msgId };
            return await HttpUtil.PostResultAsync<GroupStatus>(json, url);
        }

        /// <summary>
        /// 删除群发
        /// </summary>
        /// <param name="msgId">群发消息ID</param>
        /// <param name="articleIdx">要删除的文章在图文消息中的位置，第一篇编号为1，该字段不填或填0会删除全部文章</param>
        /// <param name="tokenOrAppId"></param>
        public static async Task<BaseRes> DeleteAsync(string msgId, int articleIdx = 0, string tokenOrAppId = "")
        {
            var url =
                $"https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var json = new { msg_id = msgId, article_idx = articleIdx };
            return await HttpUtil.PostResultAsync<BaseRes>(json, url);
        }
        /// <summary>
        /// 获取群发速度
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<SendSpeed> GetSpeedAsync(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            return await HttpUtil.PostResultAsync<SendSpeed>(null, url);
        }
        /// <summary>
        /// 设置群发速度
        /// </summary>
        /// <param name="speed">群发速度的级别，是一个0到4的整数，数字越大表示群发速度越慢。</param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<BaseRes> SetSpeedAsync(int speed, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { speed = speed };
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }
#endif
    }
}
